package com.qf.logistics.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.logistics.exceptions.MyBaseException;
import com.qf.logistics.exceptions.MyErrorCode;
import com.qf.logistics.mapper.MemberProfileMapper;
import com.qf.logistics.pojo.MemberProfile;
import com.qf.logistics.service.MemberProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * created by 赵文韬 on 2022/4/3 17:45
 */
@Service
public class MemberProfileServiceImpl implements MemberProfileService {
    private MemberProfileMapper memberProfileMapper;
    @Autowired
    public void setMemberProfileMapper(MemberProfileMapper memberProfileMapper) {
        this.memberProfileMapper = memberProfileMapper;
    }

    @Override
    public PageInfo<MemberProfile> findByKeyWord(String keyword, Long gender, Long start, Long end, int pagenum,int pagesize) {
        Assert.isTrue(keyword!=null && keyword!="",()->{
            throw new MyBaseException(MyErrorCode.PARAM_IS_NULL,"数据为空");
        } );
        Assert.isTrue(gender==1||gender==2,()->{
            throw new MyBaseException(MyErrorCode.PARAM_IS_FAIL,"性别有误" );
        } );
        Assert.isTrue(start!=null && end!=null && start<end,()->{
            throw new MyBaseException(MyErrorCode.PARAM_IS_FAIL,"参数不合法" );
        } );
        PageHelper.startPage( pagenum,pagesize);
        List<MemberProfile> memberProfile =  memberProfileMapper.findByKeyWord(keyword, gender, start, end);

        return new PageInfo(memberProfile);
    }

    @Override
    public MemberProfile findById(Long id) {
        Assert.notNull(id,()->{
            throw new MyBaseException(MyErrorCode.PARAM_IS_NULL,"id为空" );
        } );

        return memberProfileMapper.findById(id);
    }
}
